package com.dlc.felear.xiaoensale.activity;

import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.dlc.felear.xiaoensale.R;
import com.dlc.felear.xiaoensale.constant.Constant;
import com.dlc.felear.xiaoensale.deviceUtil.Serialhelper;
import com.dlc.felear.xiaoensale.deviceUtil.TopbarHelper;
import com.dlc.felear.xiaoensale.helper.Session;
import com.dlc.felear.xiaoensale.utils.NumToStringUtil;
import com.itdlc.android.library.base.BaseActivity;

import org.simple.eventbus.EventBus;
import org.simple.eventbus.Subscriber;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 设备参数设置
 */
public class DeviceParamersSettingActivity extends BaseActivity {

    private TopbarHelper topbarHelper;
    @BindView(R.id.dsp_et_xpwd)
    EditText etXpwd;
    @BindView(R.id.dsp_et_clpwd)
    EditText etClwd;
    @BindView(R.id.dsp_et_bcxs)
    EditText etBcxs;
    @BindView(R.id.dsp_et_jxck)
    EditText jxbtl;
    @BindView(R.id.dsp_et_kzbck)
    EditText etCzbck;
    @BindView(R.id.dsp_et_sfdjck)
    EditText czbBtl;
    @BindView(R.id.dsp_et_ckqck)
    EditText etjxckh;
    @BindView(R.id.dsp_et_gzmdk)
    EditText etGzmdk;
    @BindView(R.id.dsp_et_dcfsj)
    EditText etDcfsj;
    private Session session;
    private Serialhelper serialhelper;

    @Override
    protected int getLayoutRes() {
        EventBus.getDefault().register(this);
        return R.layout.activity_device_paramers_setting;
    }

    @Override
    protected void initialView() {
        topbarHelper = new TopbarHelper(this);
        topbarHelper.setNavActivity(CheckDeviceActivity.class);
        topbarHelper.setcon(R.drawable.arrow_left);
        topbarHelper.setTitle("设备参数设置");
        session = Session.getSession();
        serialhelper = Serialhelper.getInstance();
        etBcxs.setText(session.getString(Constant.WEN_DU_XI_SHU, "0"));
    }

    @Override
    protected void initData() {
        etCzbck.setText(serialhelper.getKzbSerialNo());
        etjxckh.setText(serialhelper.getJxSerialNo());
        czbBtl.setText(serialhelper.getCzbbtlValue()+"");
        jxbtl.setText(serialhelper.getJxbtlValue()+"");
        etGzmdk.setText(session.getString(Constant.GZDKSJ));
        etDcfsj.setText(session.getString(Constant.DCFYCGB));
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Subscriber(tag = Constant.EVENTBUS_TAG_JXWD)
    public void xpwendu(int value) {
//        etXpwd.setText(Integer.toString(value));
    }

    @OnClick({R.id.dsp_btn_commit1, R.id.dsp_btn_commit2, R.id.dsp_btn_commit3, R.id.dsp_btn_commit4})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.dsp_btn_commit1:
                saveWdxs();
                break;
            case R.id.dsp_btn_commit2:
                btnCommit2();
                break;
            case R.id.dsp_btn_commit3:
                btnCommit3();
                break;
            case R.id.dsp_btn_commit4:
                btnCommit4();
                break;
        }
    }

    private void btnCommit4() {
        if (!TextUtils.isEmpty(getEtValue(etGzmdk))) {
            session.setString(Constant.GZDKSJ, getEtValue(etGzmdk));
            showShortToast("修改成功");
        }
        if (!TextUtils.isEmpty(getEtValue(etDcfsj))) {
            session.setString(Constant.DCFYCGB, getEtValue(etDcfsj));
            showShortToast("修改成功");
        }
    }

    private void btnCommit2() {
        if (!TextUtils.isEmpty(getEtValue(etCzbck))
                && !TextUtils.equals(getEtValue(etCzbck), serialhelper.getKzbSerialNo())) {
            serialhelper.setKzbSerialNo(getEtValue(etCzbck));
            showShortToast("控制板串口号修改成功");
        }

        if (!TextUtils.isEmpty(getEtValue(czbBtl))) {
            serialhelper.setCzbbtl(Integer.parseInt(getEtValue(czbBtl)));
            showShortToast("控制板波特率修改成功");
        }

    }

    private void btnCommit3() {
        if (!TextUtils.isEmpty(getEtValue(etjxckh))
                && !TextUtils.equals(getEtValue(etjxckh), serialhelper.getJxSerialNo())) {
            serialhelper.setJxSerialNo(getEtValue(etjxckh));
            showShortToast("机芯串口号修改成功");
        }

        if (!TextUtils.isEmpty(getEtValue(jxbtl))) {
            serialhelper.setJxbtl(Integer.parseInt(getEtValue(jxbtl)));
            showShortToast("机芯串波特率修改成功");
        }

    }

    private void saveWdxs() {
        try {
            if (checkEtEmpty(etBcxs)) {
                showShortToast("补偿系数不能为空");
                return;
            }
            session.setString(Constant.WEN_DU_XI_SHU, NumToStringUtil.spitFloat(Float.parseFloat(getEtValue(etBcxs))));
            showShortToast("保存成功");
        } catch (Exception e) {
            showShortToast("数据异常，输入参数不符合规则");
        }

    }

    private String getEtValue(EditText et) {
        String value = et.getText().toString().trim();

        return value;
    }

    private boolean checkEtEmpty(EditText editText) {
        return TextUtils.isEmpty(editText.getText().toString().trim());
    }


}